package com.manage.jpan;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.management.modelmbean.ModelMBean;
import javax.naming.InitialContext;
import javax.security.auth.login.AccountException;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.xml.crypto.Data;

import com.mysqld.Mysqld;
import com.tools.Table;
import com.tools.Tools;

public class TeacherMan extends JPanel{
	
	int WIDTH;
	int HEIGH;
	public TeacherMan(int width,int heigh) {
		this.WIDTH=width;
		this.HEIGH=heigh;
		init();
	}
	void init() {
		this.setPreferredSize(new Dimension(WIDTH,HEIGH));
		
		Box boxHBox=Box.createVerticalBox();
		this.add(boxHBox);//建立垂直盒子
		
		Box box=Box.createHorizontalBox();//设置水平资格
		boxHBox.add(box);
		
		inil("工号",box);
		JTextField jTextField =init1(12,box);
		//inil("密码",box);
		//JTextField jTextField1 =init1(12,box);
		inil("姓名",box);
		JTextField jTextField2 =init1(12,box);
		inil("年纪",box);
		JTextField jTextField3 =init1(12,box);
		
		Box box1=Box.createHorizontalBox();//设置水平资格
		boxHBox.add(Box.createVerticalStrut(20));
		boxHBox.add(box1);
		inil("家庭地址",box1);
		JTextField jTextField4 =init1(12,box1);
		inil("授课科目",box1);
		JTextField jTextField5 =init1(12,box1);
		
		box1.add(jTextField5);
		box1.add(box.createHorizontalStrut(200));
		
		Box box2=Box.createHorizontalBox();//设置水平资格
		boxHBox.add(Box.createVerticalStrut(20));
		boxHBox.add(box2);
		
		inil("条件工号",box2);
		JTextField jTextField6 =init1(12,box2);
		
		JButton jButton=new JButton("增加老师");
		box2.add(jButton);
		box2.add(Box.createHorizontalStrut(20));
		
		JButton jButton1=new JButton("删除老师");
		box2.add(jButton1);
		box2.add(Box.createHorizontalStrut(20));
		
		
		JButton jButton2=new JButton("更改老师");
		box2.add(jButton2);
		box2.add(Box.createHorizontalStrut(20));
		
		
		JButton jButton3=new JButton("查找老师");
		box2.add(jButton3);
		box2.add(Box.createHorizontalStrut(20));
		
		
		
		//____________________________________________________________________
		Object columns[] ={"工号","姓名","年纪","家庭地址","教学科目"};
		Table t1Table=new Table(columns);//设置标题
		JTable table = t1Table.getTables();//获取表格
		JScrollPane JS = t1Table.getJScrollPane();//获取表格 滚动条
		DefaultTableModel model = t1Table.getModel();//获取表格控制
		JS.setPreferredSize(new Dimension(WIDTH-40,580));//设置整个滚动条窗口的大小
		this.add(JS);
		
		//__________________________________________________________________________________
		//增加老师
		jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//增加老师
				if(jTextField.getText().equals("")) {
					Tools.messageWindows("请输入工号");
				}else {
					//向账号里面添加账号和密码
					
					String data[]= {
							jTextField.getText(),//账号
							jTextField2.getText(),//姓名
							"12345678",//密码
							
							jTextField3.getText(),//年纪
							jTextField4.getText(),//家庭住址
							jTextField5.getText()//授课科目
							
							
							
					};
				String string="insert into teacher values(?,?,?,?,?,?)";
				int n=	Mysqld.upDate(string, data);
				if(n==1) {
					Tools.messageWindows("添加成功");
				}
				if(n==0) {
					Tools.messageWindows("添加失败，请检查输入内容");
				}
				if(n==-1) {
					Tools.messageWindows("添加失败，工号重复");
				}
					
					
				}
			}
		});
		//删除
		jButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//
				if(jTextField6.getText().equals("")) {
					Tools.messageWindows("请输入条件工号");
				}else {
					String string="delete from teacher where teacher.account=?";
					String data[]= {jTextField6.getText()};
					int n=	Mysqld.upDate(string, data);
					if(n==1) {
						Tools.messageWindows("删除成功");
					}
					if(n==0) {
						Tools.messageWindows("删除失败，请检查输工号");
					}
					if(n==-1) {
						Tools.messageWindows("删除失败，请检查输工号");
					}
				}
			}
		});
		//更改
		jButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(jTextField6.getText().equals("")) {
					Tools.messageWindows("请输入条件工号");
				}else {
					
					String string="update teacher set  account=?,name=?,age=?,address=?,project=? where account=?";
					String data[]= {
							jTextField.getText(),//账号
			
							jTextField2.getText(),//姓名
							jTextField3.getText(),//年纪
							jTextField4.getText(),//家庭住址
							jTextField5.getText(),//授课科目
							jTextField6.getText()//授课科目
							
							
					};
				
				int n=	Mysqld.upDate(string, data);
				if(n==1) {
					Tools.messageWindows("更改成功");
				}
				if(n==0) {
					Tools.messageWindows("更改失败，请检查条件工号");
				}
				if(n==-1) {
					Tools.messageWindows("更改失败，请检查条件工号");
				}
					
					
					
				}
			}
		});
		
		//查找
		jButton3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				if(jTextField6.getText().equals("")) {
					
			
				//将数据添加到表格当中
				Tools.setTableSize(table, WIDTH, Tools.addDataTable(Mysqld.QueryData("select account,name,age,address,project from teacher", null), model,5 ));
					
				}else {
					String data[]= {jTextField6.getText()};
					Tools.setTableSize(table, WIDTH, Tools.addDataTable(Mysqld.QueryData("select account,name,age,address,project from teacher where account=?",  data), model,5 ));
				ResultSet rs = Mysqld.QueryData("select account,name,age,address,project from teacher where account=?",  data);
				try {
					while(rs.next()) {
						//向文本写入数据
						jTextField.setText(rs.getString(1));
						jTextField2.setText(rs.getString(2));
						jTextField3.setText(rs.getString(3));
						jTextField4.setText(rs.getString(4));
						jTextField5.setText(rs.getString(5));
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
					
				}
			}
		});
		
		
		
	}
	//初始化标签与文本
	void inil(String sr,Box box) {
		JLabel jLabel=new JLabel(sr);
		
		box.add(jLabel);
		box.add(Box.createHorizontalStrut(20));
	}
	JTextField init1(int h,Box box) {
		
		JTextField jTextField=new JTextField(h);
		box.add(jTextField);
		box.add(Box.createHorizontalStrut(20));
		return jTextField;
	}
	
	
	
}
